<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        //echo "后台首页";
        $this->display();
    }

    public function welcome()
    {	
    	//如果已经有缓存走缓存  否则走生成(设置)缓存
    	//缓存默认以文件的形式存在App/Runtime/Temp
    	if (S('welcomeData')) {
    		var_dump('已经缓存,不刷新数据表,节省资源');
    		$data = S('welcomeData');
    	}else{
    		var_dump('生成缓存,刷新数据表');
    		//折行这个类的countData得到return的$data数组
    		$data = $this->countData();
    		//S内置方法  设置缓存 名、需要缓存的值、缓存时间
    		S('welcomeData',$data,3600);
    	}
    	
		$this->assign('data',$data);
    	$this->display();
    }

    public function countData()
    {
    	//Date() 函数把时间戳格式化为更易读的日期和时间
    	$str = date('y-m-d'); //不带'时分秒'参数 返回今天日期 (以此字符串日期可转成今天凌晨时间戳)
    	var_dump($str);  //string '17-04-11' 
    	
    	$time=strtotime(date('y-m-d')); //今天strtotime把字符串时间转成时间戳 不带'时分秒'就返回今天凌晨时间戳$time
    	var_dump($time); //int 1491840000

    	//var_dump(strtotime("-1 day",$time)); 
    	$yesterday=strtotime("-1 day",$time);//用今天凌晨时间戳$time-1天的时间戳得到昨天凌晨时间戳 不加$time为1491811182  2017/4/10 15:59:42   加$time为1491753600  2017/4/10 0:0:0
    	var_dump($yesterday);
    	
    	$week=strtotime("-7 day",$time); //7天前或一个星期前的凌晨时间戳  1491321600
    	var_dump($week);

    	$monthago=strtotime("-30 day",$time); //30天前或一个月前的凌晨时间戳  1489334400
    	var_dump($monthago);


    	$data=array();
    	//根据id查询所有数据 返回总id量
    	$sql = "select count(id) cn from question";
    	$res = M()->query($sql);
    	$data['总数'][] = M()->query($sql)[0]['cn'];
    	/*array (size=1)
		  0 => 
		    array (size=1)
		      'cn' => string '10' (length=2)*/
		$sql = "select count(id) cn from answer";
    	$res = M()->query($sql);
    	$data['总数'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from user";
    	$res = M()->query($sql);
    	$data['总数'][] = M()->query($sql)[0]['cn'];
    	


    	//查询今天数据 先拿到今天凌晨时间 若当前id时间>今天凌晨时间=今天
    	$sql = "select count(id) cn from question where create_time>'{$time}'";
    	$res = M()->query($sql);
    	$data['今日'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from answer where create_time>'{$time}'";
    	$res = M()->query($sql);
    	$data['今日'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from user where create_time>'{$time}'";
    	$res = M()->query($sql);
    	$data['今日'][] = M()->query($sql)[0]['cn'];



    	//查询昨天数据 先拿到昨天凌晨时间 若当前id时间>昨天凌晨时间并且当前id时间<今天凌晨时间=昨天
    	$sql = "select count(id) cn from question where create_time>'{$yesterday}' and create_time<'{$time}'";
    	$res = M()->query($sql);
    	$data['昨日'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from answer where create_time>'{$yesterday}' and create_time<'{$time}'";
    	$res = M()->query($sql);
    	$data['昨日'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from user where create_time>'{$yesterday}' and create_time<'{$time}'";
    	$res = M()->query($sql);
    	$data['昨日'][] = M()->query($sql)[0]['cn'];



    	//查询一星期内数据 先拿到7天前凌晨时间 若当前时间>7天前凌晨时间=本周或一星期内数据
    	$sql = "select count(id) cn from question where create_time>'{$week}'";
    	$res = M()->query($sql);
    	$data['本周'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from answer where create_time>'{$week}'";
    	$res = M()->query($sql);
    	$data['本周'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from user where create_time>'{$week}'";
    	$res = M()->query($sql);
    	$data['本周'][] = M()->query($sql)[0]['cn'];



    	//查询一个月内数据 先拿到30天前凌晨时间 若当前时间>30天前凌晨时间=本月或一个月内数据
    	$sql = "select count(id) cn from question where create_time>'{$monthago}'";
    	$res = M()->query($sql);
    	$data['本月'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from answer where create_time>'{$monthago}'";
    	$res = M()->query($sql);
    	$data['本月'][] = M()->query($sql)[0]['cn'];
    	$sql = "select count(id) cn from user where create_time>'{$monthago}'";
    	$res = M()->query($sql);
    	$data['本月'][] = M()->query($sql)[0]['cn'];
    	/*var_dump($data);
    	array (size=5)
		  '总数' => 
		    array (size=3)
		      0 => string '18' (length=2)
		      1 => string '14' (length=2)
		      2 => string '10' (length=2)
		  '今日' => 
		    array (size=3)
		      0 => string '0' (length=1)
		      1 => string '0' (length=1)
		      2 => string '0' (length=1)*/

		
    	//Date() 函数把时间戳格式化为更易读的日期和时间 date(format,timestamp) 
			//echo "今天是 " . date("Y/m/d h/i/s") . "<br>";
			//echo "今天是 " . date("Y.m.d h.i.s") . "<br>";
			//echo "今天是 " . date("Y-m-d h:i:s") . "<br>";
			//echo "今天是 " . date("l");
			//echo "今天是 " . date("Y-m-d h:i:s",1474272975);
		#strtotime() strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳（自 January 1 1970 00:00:00 GMT 起的秒数）。
		    //echo(strtotime("now") . "<br>");
			//echo(strtotime("15 October 1980") . "<br>");
			//echo(strtotime("+5 hours") . "<br>");
			//echo(strtotime("+1 week") . "<br>");
			//echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
			//echo(strtotime("next Monday") . "<br>");
			//echo(strtotime("last Sunday"));
		//getdate() 函数返回某个时间戳或者当前本地的日期/时间的日期/时间信息
		return $data;
    }
}
